#ifndef __AUTO_SINGLETON_HH__
#define __AUTO_SINGLETON_HH__

/**
 * @author Olgierd Humenczuk olgierdh@gmail.com
 */

#include <boost/utility.hpp>

#include "ThreadSafePolicy.hh"

namespace Common
{

/**
 * @brief AutoSingleton pattern
 */
template< typename Type, class ThreadSafePolicy = ThreadSecured >
class AutoSingleton : public boost::noncopyable
{
	public:
		/**
		 * @brief returns reference to an object of given type
		 */
		static Type& getRef( void )
		{
			typename ThreadSafePolicy::ScopedLock sl( AutoSingleton::iMutex );
			static Type object;
			return object;
		}

		/**
		 * @brief returns pointer to an object of type
		 */
		static Type* getPtr( void )
		{
			return &( getRef() );
		}

	protected:
		AutoSingleton( void )
		{
		}

	private:
		static typename ThreadSafePolicy::Mutex iMutex; /// data mutex depends on thread safe policy
};

template< typename Type, class ThreadSafePolicy >
typename ThreadSafePolicy::Mutex AutoSingleton< Type, ThreadSafePolicy >::iMutex;

} //namespace Common

#endif
